from django.db import models

# Create your models here.
TEMPLATE_CHOICES = (
    ('', 'Select'),
    ('300x250', '300x250'),
    ('728x90', '728x90'),
    ('468x60', '468x60'),  
    ('160x600', '160x600'),
    ('120x600', '120x600'),
    ('180x150', '180x150'),
    ('336x280', '336x280'),  
    ('Custom', 'Custom')
)

EXPAND_CHOICES = (
    ('none', 'Select'),
    ('left', 'left'),
    ('right', 'right'),
    ('down', 'down'),  
    ('up', 'up'),
)

class Concept(models.Model):
    active = models.BooleanField(default=True)
    date = models.DateTimeField(auto_now=True)    
    name = models.CharField(max_length = 100)
    banners = models.ManyToManyField('Banner', blank=True)
    position = models.IntegerField(default=7777)
    show_delivery = models.BooleanField(default=False)
    delivery = models.FileField('Concept Delivery', upload_to='deliveries/concepts/', blank=True)
    
    def __unicode__(self):
        return '%s : %s' % (self.name, self.date)
    
class Campaign(models.Model):
    active = models.BooleanField(default=True)    
    date = models.DateTimeField(auto_now=True)    
    name = models.CharField(max_length = 100)
    slug = models.SlugField(max_length = 100)
    show_delivery = models.BooleanField(default=False)
    delivery = models.FileField('Campaign Delivery', upload_to='deliveries/campaigns/', blank=True)
    concepts = models.ManyToManyField(Concept, blank=True)
    
    
class Banner(models.Model):
    active = models.BooleanField(default=True)
    date = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length = 100)
    banner = models.FileField('30k/Front Load swf', upload_to='banners/', max_length=255,)
    panel = models.FileField('Back Load swf', upload_to='banners/', blank=True,  max_length=255,)
    page_template = models.ForeignKey('Page_Template')
    size = models.CharField(choices=TEMPLATE_CHOICES, max_length=100)
    width = models.PositiveIntegerField(default=0)
    height = models.PositiveIntegerField(default=0)
    is_expandable = models.BooleanField(default=False)
    direction = models.CharField(choices=EXPAND_CHOICES, max_length=100, blank=True)
    expandable_width = models.PositiveIntegerField(default=0)
    expandable_height = models.PositiveIntegerField(default=0)
    
    def save(self):
        if self.size == '300x250':
            self.width = 300
            self.height = 250
            
        elif self.size == '728x90':
            self.width = 728
            self.height = 90
        
        elif self.size == '468x60': 
            self.width = 468
            self.height = 60
        
        elif self.size == '160x600':
            self.width = 160
            self.height = 600
        
        elif self.size == '120x600':
            self.width = 120
            self.height = 600
        
        elif self.size == '180x150':
            self.width = 120
            self.height = 600
            
        elif self.size == '336x280':   
            self.width = 336
            self.height = 280 
        
        super(Banner, self).save()
                
    class Meta:
        verbose_name = 'Banner'
        verbose_name_plural = 'Banners'
        
    def __unicode__(self):
        return '%s : %s' % (self.name, self.size)


class Page_Template(models.Model):
    active = models.BooleanField(default=True)    
    name = models.CharField(max_length = 100)
    size = models.CharField(choices=TEMPLATE_CHOICES, max_length=100,)
    image = models.ImageField(upload_to='page-templates/images', blank=True, help_text="Background Image for page layout(cnn.com, etc)", max_length=255,)
    x_position = models.PositiveIntegerField(default=15, help_text="X Postion of banner on page - need to adjust when there is a background image",)
    y_position = models.PositiveIntegerField(default=15, help_text="Y Postion of banner on page - need to adjust when there is a background image")
    width = models.PositiveIntegerField(default=0)
    height = models.PositiveIntegerField(default=0)
    
    def save(self):
        if self.size == '300x250':
            self.width = 300
            self.height = 250
            
        elif self.size == '728x90':
            self.width = 728
            self.height = 90
        
        elif self.size == '468x60': 
            self.width = 468
            self.height = 60
        
        elif self.size == '160x600':
            self.width = 160
            self.height = 600
        
        elif self.size == '120x600':
            self.width = 120
            self.height = 600
        
        elif self.size == '180x150':
            self.width = 120
            self.height = 600
            
        elif self.size == '336x280':   
            self.width = 336
            self.height = 280
        
        super(Page_Template, self).save()
        
    class Meta:
        verbose_name = 'Page Template'
        verbose_name_plural = 'Page Templates'        
            
            
    def __unicode__(self):
        return '%s : %s' % (self.size, self.name,)
    